package com.wyg.blog.service.serviceimpl;

import com.wyg.blog.model.user.Role;
import com.wyg.blog.model.user.SysUser;
import com.wyg.blog.model.user.User;
import com.wyg.blog.service.user.RoleService;
import com.wyg.blog.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: WYG
 * @time: 2020/6/22 16:45
 */
@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UserService userService;

    @Autowired
    RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = userService.selectUserByUsername(s);
        if(user == null){
            throw new UsernameNotFoundException(String.format("%s.这个用户不存在", s));
        } else {
            List<Role> roles = roleService.selectRoleByUid(user.getId());
            SysUser sysUser = new SysUser(user.getId(),user.getUsername(),user.getPassword(),user.isStatus(),roles);
            return sysUser;
        }
    }
}
